// pages/home/home.js
var util = require('../../utils/util.js')
var config = require('../../utils/config.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getPhoneColor: "#FF74B1",
    getDeviceColor: "#666666",
    numberType: 1,
    isShowGetPhone: true,
    productNumber: "",
    device_list: [],
    isHavePermission: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var premission = wx.getStorageSync('premission');

    if (premission != "" && premission) {
      this.setData({
        isHavePermission: premission
      });
    } else {

      var that = this;
      var phoneNumber = wx.getStorageSync('phoneNumber');
      if (phoneNumber != null && phoneNumber != "") {
        wx.request({
          url: config.service.checkAuth,
          data: {
            timestamp: "12345678",
            sign: "87575e3ff9346c3eb7fc68e46758e066",
            username: phoneNumber
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function(res) {
            var resCode = res.data.code;
            if (resCode == 200) {
              wx.setStorageSync('premission', true);
              that.setData({
                isHavePermission: true
              });
            }else  {
              wx.setStorageSync('premission', false);
            }
          }
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  getProductNumber: function(e) {
    this.setData({
      productNumber: e.detail.value
    })
  },

  searchDevice: function(e) {
    // 获取产品名称,获取产品序号
    // 获取登录账号
    var productNumber = this.data.productNumber;
    var numberType = this.data.numberType;
    if (productNumber != "") {
      if (numberType == 1) {
        this.searchFoPhone(productNumber);
      } else {
        this.searchFoDeviceNumber(productNumber);
      }
    } else {
      this.myShowToast("请输入手机号码或者设备编码");
    }
  },
  text_switch: function(e) {
    var numberType = e.currentTarget.dataset.type;
    this.setData({
      getPhoneColor: (numberType == 1 ? "#FF74B1" : "#666666"),
      getDeviceColor: (numberType == 2 ? "#FF74B1" : "#666666"),
      isShowGetPhone: numberType == 1,
      numberType: numberType,
      productNumber: ""
    });
  },
  searchFoPhone: function(phone) {
    var that = this;
    wx.showLoading({
      title: '查询中',
      mask: true
    })

    wx.request({
      url: config.service.seatchMobile,
      data: {
        timestamp: "12345678",
        sign: "4d1cf67b55ebcf452da16493fe889bea",
        username: phone
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        wx.hideLoading();
        var resData = res.data;
        var resCode = resData.code;
        if (resCode == 200) {
          var resArray = resData.data;
          that.analyticalData(resArray);
        } else {
          that.myShowToast(resData.message);
          that.analyticalData([]);
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    })

  },
  searchFoDeviceNumber: function(deviceNumber) {
    var that = this;
    wx.showLoading({
      title: '查询中',
      mask: true
    })

    wx.request({
      url: config.service.searchMac,
      data: {
        timestamp: "12345678",
        sign: "146a56b469615b9179e144eef3296943",
        mac: deviceNumber
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        wx.hideLoading();
        var resData = res.data;
        var resCode = resData.code;
        if (resCode == 200) {
          var resArray = resData.data;
          that.analyticalData(resArray);
        } else {
          that.myShowToast(resData.message);
          that.analyticalData([]);
        }
      },
      fail: function(res) {
        wx.hideLoading();
      }
    })
  },
  analyticalData: function(resData) {
    // 解析数据
    for (var i = 0; i < resData.length; i++) {
      var device = resData[i];
      var factory = device.factory;
      if (factory == null || factory == "") {
        factory = "H001";
      }
      var mac = device.mac;
      device['devicename'] = factory + "-" + mac.substr(mac.length - 6);
    }

    this.setData({
      device_list: resData
    });
  },
  unbinddevice: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var deviceList = this.data.device_list;
    var deviceId = deviceList[index].id;

    wx.showLoading({
      title: '解绑中...',
      mask: deviceId
    })

    wx.request({
      url: config.service.unbinding,
      data: {
        timestamp: "12345678",
        sign: "d81dc4187036437505fe6b335c271f93",
        id: deviceId
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        wx.hideLoading();
        var resData = res.data;
        var resCode = resData.code;
        if (resCode == 200) {
          that.myShowToast("解绑成功");
          deviceList.splice(index, 1);
          that.setData({
            device_list: deviceList
          });
        } else {
          that.myShowToast("解绑失败");
        }
      },
      fail: function(res) {
        wx.hideLoading();
        that.myShowToast("解绑失败");
      }
    })
  },
  myShowToast: function(toastContent) {
    wx.showToast({
      title: toastContent + "",
      icon: 'none',
      duration: 1500
    })
  }
})